package com.hzcx.app.simplechat.ui.setting;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.base.BaseActivity;
import com.hzcx.app.simplechat.ui.setting.contract.AddAutoSendMsgContract;
import com.hzcx.app.simplechat.ui.setting.event.AddAutoSendMsgEvent;
import com.hzcx.app.simplechat.ui.setting.presenter.AddAutoSendMsgPresenter;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.util.glide.GlideUtils;
import com.hzcx.app.simplechat.util.photo.PhotoUtil;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;
import com.luck.picture.lib.listener.OnResultCallbackListener;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 添加自动回复消息-图文
 */
public class AddAutoSendMsgImageActivity extends BaseActivity<AddAutoSendMsgPresenter> implements AddAutoSendMsgContract.View, OnResultCallbackListener<LocalMedia> {

    public static final String INTENT_ID = "INTENT_ID";
    public static final String INTENT_CONTENT = "INTENT_CONTENT";
    public static final String INTENT_IMAGE = "INTENT_IMAGE";

    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.iv_title_right)
    ImageView ivTitleRight;
    @BindView(R.id.tv_title_right)
    TextView tvTitleRight;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.iv_img)
    ImageView ivImg;

    private String imgPath;
    private String id;

    private boolean isCanAdd = false;

    @Override
    public int getLayout() {
        return R.layout.activity_add_auto_send_msg_image;
    }

    @Override
    public void initPresenter() {
        mPresenter=new AddAutoSendMsgPresenter();
    }

    @Override
    public void initView() {
        tvTitle.setText("图文内容");
        tvTitleRight.setText("完成");

        etContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                checkImgAndContent();
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
    }

    @Override
    public void initData() {
        id = getIntent().getStringExtra(INTENT_ID);
        if (EmptyUtils.isNotEmpty(getIntent().getStringExtra(INTENT_CONTENT))) {
            etContent.setText(getIntent().getStringExtra(INTENT_CONTENT));
        }
        if (EmptyUtils.isNotEmpty(getIntent().getStringExtra(INTENT_IMAGE))) {
            GlideUtils.loadImgAsCenter(this, getIntent().getStringExtra(INTENT_IMAGE), ivImg);
        }
    }

    @OnClick({R.id.tv_title_right, R.id.iv_img})
    public void viewOnClick(View view) {
        switch (view.getId()) {
            case R.id.tv_title_right:
                if (!isCanAdd) {
                    showError("内容或图片不可为空");
                    return;
                }
                if (EmptyUtils.isEmpty(imgPath)) {
                    mPresenter.addAutoChatMessage(this, 2, etContent.getText().toString(), getIntent().getStringExtra(INTENT_IMAGE), id);
                } else {
                    mPresenter.upLoadImg(this, imgPath);
                }
                break;

            case R.id.iv_img:
                PhotoUtil.showPhoto(this, PictureMimeType.ofImage(), PictureConfig.SINGLE, false, false, null, this);
                break;
        }
    }

    @Override
    public void addSuccess() {
        EventBus.getDefault().post(new AddAutoSendMsgEvent());
        showError("添加成功");
        finish();
    }

    /**
     * 图片上传成功
     *
     * @param url
     */
    @Override
    public void imgLoadResult(String url) {
        mPresenter.addAutoChatMessage(this, 2, etContent.getText().toString(), url, id);
    }

    /**
     * 图片选择返回
     *
     * @param result
     */
    @Override
    public void onResult(List<LocalMedia> result) {
        try {
            imgPath = PhotoUtil.getPath(result.get(0));
            GlideUtils.loadImgAsCenter(this, imgPath, ivImg);
            checkImgAndContent();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onCancel() {

    }

    private void checkImgAndContent() {
        if (EmptyUtils.isNotEmpty(imgPath) && etContent.getText().toString().length() > 0) {
            isCanAdd = true;
        } else {
            isCanAdd = false;
        }
        if (EmptyUtils.isNotEmpty(getIntent().getStringExtra(INTENT_IMAGE)) && etContent.getText().toString().length() > 0) {
            isCanAdd = true;
        }
    }
}
